﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.Model;
using System.Linq.Expressions;
using System.Linq.Dynamic;

namespace WXYW
{
    public partial class ListRepairFormOverDue : System.Web.UI.Page
    {
        public int RecordsNumber { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindProduct();
            }
        }

        private void BindProduct()
        {
            try
            {
                ApplicationProductService service = new ApplicationProductService();
                List<ApplicationProductOverDueModel> models = service.GetApplicationProductByRoleAndUserID(ContextService.CurrentAccount.UserType.ID, ContextService.CurrentAccount.UserID, keyWordTextBox.Text.Trim());

                if (Session["OrderExpression"] != null && Session["OrderDirection"] != null)
                {
                    models = models.AsQueryable().OrderBy(string.Format("{0} {1}", Session["OrderExpression"].ToString(), Session["OrderDirection"].ToString())).ToList();
                }
                RecordsNumber = models.Count;
                myApplicationGridView.DataSource = models;
                myApplicationGridView.DataBind();
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "ListRepairFormOverDue.aspx::BindProduct");
                ClientScript.RegisterStartupScript(this.GetType(), "tip", string.Format("alert('信息绑定发生错误，请截图保存该错误信息并联系管理员！错误原因：{0}');", ex.Message), true);
            }
        }

        protected void myApplicationGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < myApplicationGridView.PageCount)
            {
                myApplicationGridView.PageIndex = e.NewPageIndex;
            }
            
            BindProduct();
        }

        protected void pageDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList pageDDL = sender as DropDownList;
            myApplicationGridView.PageIndex = Convert.ToInt32(pageDDL.SelectedIndex);
            BindProduct();
        }

        protected void myApplicationGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                int day = (e.Row.DataItem as ApplicationProductOverDueModel).RepairDay;
                Label overduelabel = e.Row.FindControl("overdueLabel") as Label;
                if (day < 4)
                {
                    overduelabel.Text = "!";
                }
                else if (day < 7)
                {
                    overduelabel.Text = "!!";
                }
                else
                {
                    overduelabel.Text = "!!!";
                }

                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
            if (e.Row.RowType == DataControlRowType.Pager)
            {
                DropDownList pageDDL = e.Row.FindControl("pageDDL") as DropDownList;
                for (int index = 1; index <= myApplicationGridView.PageCount; index++)
                {
                    pageDDL.Items.Add(index.ToString());
                }
                pageDDL.SelectedValue = (myApplicationGridView.PageIndex + 1).ToString();
            }
        }

        protected void myApplicationGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                if (Session["OrderExpression"] != null)
                {
                    if (e.SortExpression == Session["OrderExpression"].ToString())
                    {
                        string sortDirection = "asc";
                        if (Session["OrderDirection"] != null)
                        {
                            sortDirection = Session["OrderDirection"].ToString();
                            if (sortDirection.Equals("asc"))
                            {
                                Session["OrderDirection"] = "desc";
                            }
                            else
                            {
                                Session["OrderDirection"] = "asc";
                            }
                        }
                        else
                        {
                            Session["OrderDirection"] = "asc";
                        }
                    }
                    else
                    {
                        Session["OrderExpression"] = e.SortExpression;
                        Session["OrderDirection"] = "asc";
                    }
                }
                else
                {
                    Session["OrderExpression"] = e.SortExpression;
                    Session["OrderDirection"] = "asc";
                }
                BindProduct();
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "ListRepairFormOverDue.aspx::myApplicationGridView_Sorting");
                ClientScript.RegisterStartupScript(this.GetType(), "tip", string.Format("alert('信息绑定发生错误，请截图保存该错误信息并联系管理员！错误原因：{0}');", ex.Message), true);
            }
        }

        protected void myApplicationGridView_DataBound(object sender, EventArgs e)
        {
            if (myApplicationGridView.BottomPagerRow != null)
            {
                myApplicationGridView.BottomPagerRow.Visible = true;
            }
        }

        protected void searchLinkButton_Click(object sender, EventArgs e)
        {
            BindProduct();
        }

        protected void resetLinkButton_Click(object sender, EventArgs e)
        {
            keyWordTextBox.Text = "";
            BindProduct();
        }

        protected void rmalnk_Click(object sender, EventArgs e)
        {
            LinkButton lnkBtn = sender as LinkButton;
            string back_url = Request.Url.ToString();
            string url = string.Format("RMALinkDetail.aspx?APPLICATIOM_ID={0}&" + Constants.QUERYSTR_BACK_URL + "={1}",
                lnkBtn.CommandArgument, back_url);
            Response.Redirect(url);
        }
    }
}
